package com.mumulx.check.service.service.impl;

import com.mumulx.check.mapper.SubmitFileRecordMapper;
import com.mumulx.check.model.SubmitFileRecord;
import com.mumulx.check.model.SubmitRecord;
import com.mumulx.check.redis.entity.PartTestReturnBoxed;
import com.mumulx.check.redis.util.FastDfsClientUtil;
import com.mumulx.check.service.service.SubmitFileRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.*;

/*
 *  Created by IntelliJ IDEA.
 *  User: 木木
 *  Date: 2020/4/28
 *  Time: 22:48
 */
@Service("submitFileRecordService")
@Slf4j
public class SubmitFileRecordServiceImpl implements SubmitFileRecordService {


    @Resource
    SubmitFileRecordMapper submitFileRecordMapper;
    @Resource
    FastDfsClientUtil fastDfsClientUtil;

    //@Resource
    //HelpDev helpDev;

    /*插入一个SubmitFileRecord*/
    @Override
    public void addSFS(SubmitFileRecord submitFileRecord) {

    }

    /*增加 一次测试记录的所有文件  的测试集合 记录；插入SubmitFileRecord集合*/
    @Override
    public void addSFSList(int srId, Map<String, String> fileNames) {

        List<SubmitFileRecord> sfrs = new ArrayList<>();
        /*文件名集合*/
        Set<String> strings = fileNames.keySet();
        for (String prtName : strings) {
            String prtSaveName = fileNames.get(prtName);

            SubmitFileRecord submitFileRecord = new SubmitFileRecord();
            submitFileRecord.setSfrFilePath(prtSaveName);
            submitFileRecord.setSfrFileName(prtName);
            submitFileRecord.setSfrsrId(srId);
            sfrs.add(submitFileRecord);
        }
        submitFileRecordMapper.addSFSList(sfrs);
    }

    @Override
    public Map<Integer, List<SubmitFileRecord>> queryFileRecordListBysridList(List<SubmitRecord> submitRecords) {

        Map<Integer, List<SubmitFileRecord>> sfrs = new HashMap<>();

        for (SubmitRecord submitRecord : submitRecords) {
            int srId = submitRecord.getSrId();
            List<SubmitFileRecord> submitFileRecords = submitFileRecordMapper.queryFileRecordListBysrid(srId);
            sfrs.put(srId, submitFileRecords);
        }
        return sfrs;
    }

    @Override
    public List<SubmitFileRecord> queryFileRecordListBysrid(int srid) {
        return submitFileRecordMapper.queryFileRecordListBysrid(srid);
    }

    @Override
    public List<SubmitFileRecord> queryFileRecordsBysrid(int srId) {
        return submitFileRecordMapper.queryFileRecordListBysrid(srId);
    }


    /*根据测试记录id，删除此次测试记录的文件记录*/
    @Override
    public void deleteFileRecordBySrId(Integer srId) {
        //获取测试文件记录集合
        List<SubmitFileRecord> submitFileRecords = submitFileRecordMapper.queryFileRecordListBysrid(srId);
        String sfrXslFile = submitFileRecords.get(0).getSfrXslFile();
        submitFileRecords.forEach(submitFileRecord -> {
            //删除fastdfs服务器上的文件
            List<String> files = Arrays.asList(submitFileRecord.getSfrPngFile(),
                    submitFileRecord.getSfrXmlFile(),
                    //submitFileRecord.getSfrXslFile(),
                    submitFileRecord.getSfrFilePath());
            fastDfsClientUtil.delete_file(files);
        });
        fastDfsClientUtil.delete_file(sfrXslFile);
        log.info("开始删除测试文件记录");
        //删除数据库中的数据
        submitFileRecordMapper.deleteFileRecordBySrId(srId);
        log.info("测试文件记录删除成功");

    }

    @Override
    public SubmitFileRecord queryFileRecordBySfrId(int sfrId) {
        return submitFileRecordMapper.queryFileRecordBySfrId(sfrId);
    }

    @Override
    public void updatesfrResultFileById(List<PartTestReturnBoxed> partTestReturnBoxeds) {

        for (PartTestReturnBoxed prb : partTestReturnBoxeds) {
            submitFileRecordMapper.updatesfrResultFileById(prb.getSfrID(),prb.getXmlFilePath(),prb.getPngFilePath(),prb.getXslFilePath());

        }
    }



}
